package com.qf.day05.homework;

import java.util.Random;
import java.util.Scanner;

public class Menu {
    UserManager userManager = new UserManager();
    public void menu1() {
        System.out.println("-----欢迎进入千锋用户管理系统-----");
        Scanner sc = new Scanner(System.in);
        while(true) {
            System.out.println("1、登录\t2、注册\t3、退出");
            int option = sc.nextInt();
            switch (option) {
                case 1:
                    System.out.println("请输入用户名");
                    String username = sc.next();
                    System.out.println("请输入密码");
                    String password = sc.next();
                    String code = code();
                    System.out.println("请输入验证码"+code);
                    String code1 = sc.next();
                    if(code.equalsIgnoreCase(code1)) {
                        if(userManager.login(username, password)) {
                            //进入二级菜单
                            System.out.println("登录成功");
                        }else {
                            System.out.println("登录失败");
                        }
                    }else {
                        System.out.println("验证码错误");
                    }

                    break;
                case 2:
                    System.out.println("请输入用户名");
                    String username1 = sc.next();
                    System.out.println("请输入密码");
                    String password1 = sc.next();
                    if( userManager.register(username1, password1) ) {
                        System.out.println("注册成功");
                    }else {
                        System.out.println("注册失败");
                    }
                    break;
                case 3:
                    System.out.println("欢迎下次使用");
                    return;
                default:
                    System.out.println("输入有误，请重新输入");
                    break;
            }

        }
    }
    /**
     * 随机生成验证码
     */
    public String code() {
        String[] s = {"a","b","c","d","1","2","3","4"};
        Random r = new Random();
        String code = "";
        for (int i = 0; i < 4; i++) {
            int index = r.nextInt(s.length);
            code = code + s[index];
        }
        return code;
    }
}